package com.example.demo.config;

import com.example.demo.config.response.ResponseResultInterceptor;
import com.example.demo.config.session.AuthorizationInterceptor;
import com.example.demo.config.session.LoginUserHandlerMethodArgumentResolver;
import com.example.demo.config.weblog.LogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author
 * 拦截器注册
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    ResponseResultInterceptor responseResultInterceptor;
    @Autowired
    AuthorizationInterceptor authorizationInterceptor;
    @Autowired
    LogInterceptor logInterceptor;
    @Autowired
    LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加返回信息拦截器
        registry.addInterceptor(responseResultInterceptor)
            .addPathPatterns("/**")
            .excludePathPatterns("/doc.html")
        ;
        // 添加权限验证拦截器
        registry.addInterceptor(authorizationInterceptor)
                .addPathPatterns("/user")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/swagger-resources")
                .excludePathPatterns("/swagger-resources/configuration/ui")
        ;
        // 添加日志拦截器
        registry.addInterceptor(logInterceptor);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置拦截器访问静态资源
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
